<?php

namespace App\Admin\Controllers;

use App\Admin\Repositories\Commission;
use App\Models\School;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;

class CommissionController extends AdminController
{
    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new Commission('school'), function (Grid $grid) {
            if (Admin::user()->school_id) {
                $grid->model()->where('school_id', Admin::user()->school_id);
            }
            $grid->column('id')->sortable();
            $grid->column('school.name');
            $grid->column('price');
            $grid->column('service_charge');
            $grid->column('ordering_fee');
            $grid->column('created_at');
            $grid->column('updated_at')->sortable();

            $grid->filter(function (Grid\Filter $filter) {
                if (is_null(Admin::user()->school_id)) {
                    $filter->equal('school_id')->width(3)->select(School::all()->pluck('name', 'id'));
                }
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new Commission(), function (Show $show) {
            $show->field('id');
            $show->field('money');
            $show->field('service_charge');
            $show->field('ordering_fee');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new Commission(), function (Form $form) {
            if (Admin::user()->school_id) {
                $form->hidden('school_id')->value(Admin::user()->school_id);
            } else {
                $form->select('school_id')->options(School::get()->pluck('name', 'id'));
            }
            $form->decimal('price');
            $form->decimal('service_charge');
            $form->decimal('ordering_fee');
        });
    }
}
